﻿/*
	AirLogger V1.0.1
	@author: Sidney de Koninig, 2008

	Make sure you have the correct File.path.
	You can change this bwelow if you like, depending on your project needs
	var appDir:File 		= File.applicationDirectory;
	var appStoreDir:File 	= File.applicationStorageDirectory;
	var desktopDir:File 	= File.desktopDirectory;
	var docDir:File 		= File.documentsDirectory;
	var userDir:File 		= File.userDirectory;
	var rootDirArr:Array 	= File.getRootDirectories();
	
	TODO:Confine to application sanbox, now i have r/w acces to everything. 
	(Even to directories that i have no right to)
	
*/

package com.buzzfactory.logger {
	
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.filesystem.File;
	
	public class AirLogger {	
		
		private static var _file		:File;
		private static var _fileStream 	:FileStream;
		private static var _fileName	:String;
		private static var _version		:String = "1.0.0";
		
		public static function init(fileName:String, emptyLog:Boolean = true):void
		{
			AirLogger._fileName		=	fileName;
			AirLogger._file			= 	File.applicationStorageDirectory.resolvePath( AirLogger._fileName );
			AirLogger._fileStream	= 	new FileStream();
			
			if(emptyLog && AirLogger._file.exists)
			{
				AirLogger.clearLog();
			} 
			AirLogger._fileStream.open( AirLogger._file, FileMode.APPEND );
			AirLogger.log("AirLogger, version " + AirLogger.getVersion());
		}
		
		public static function close():void
		{
			AirLogger._fileStream.close();
		}
		public static function log (s:* ):void
		{
			if (s == null){
				s = "";
			}
			
			AirLogger._fileStream.writeUTFBytes( s.toString() + "\n");
			trace( s.toString());
		}
		//
		public static function clearLog():void
		{
			AirLogger._fileStream.open( AirLogger._file, FileMode.WRITE );
			AirLogger._fileStream.writeUTFBytes("");
			AirLogger._fileStream.close();
		}
		//
		public static function newLine():void
		{
			AirLogger.log("");
		} 
		public static function getVersion():String
		{
			return _version;
		}		
	}
}